package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.domain.po.Emp;
import com.itheima.domain.query.EmpQueryParam;
import com.itheima.domain.vo.PageResult;
import com.itheima.domain.vo.Result;
import com.itheima.service.EmpService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "员工管理", description = "员工管理相关接口")
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @Operation(summary = "分页查询员工", description = "根据条件分页查询员工列表")
    @GetMapping
    public Result page(@Parameter(description = "查询参数") EmpQueryParam param) {
        log.info("员工列表查询条件：{}", param);
        PageResult<Emp> pageEmp = empService.pageEmp(param);
        return Result.success(pageEmp);
    }

    @Operation(summary = "新增员工", description = "新增员工信息")
    @Log
    @PostMapping
    public Result save(@Parameter(description = "员工信息") @RequestBody Emp emp) {
        log.info("新增员工，员工信息：{}", emp);
        empService.saveEmpAndExpr(emp);
        return Result.success();
    }

    @Operation(summary = "批量删除员工", description = "根据ID列表批量删除员工")
    @Log
    @DeleteMapping
    public Result delete(@Parameter(description = "员工ID列表") @RequestParam List<Integer> ids) {
        log.info("批量删除学生，{}", ids);
        empService.removeBatchByIdsAndExpr(ids);
        return Result.success();
    }

    @Operation(summary = "根据ID查询员工", description = "根据员工ID查询员工详细信息")
    @GetMapping("/{id}")
    public Result getById(@Parameter(description = "员工ID") @PathVariable Integer id) {
        log.info("根据ID查询员工信息，id：{}", id);
        Emp emp = empService.getByIdAndExpr(id);

        return Result.success(emp);
    }

    @Operation(summary = "修改员工", description = "更新员工信息")
    @Log
    @PutMapping
    public Result update(@Parameter(description = "员工信息") @RequestBody Emp emp) {
        log.info("修改员工信息，员工信息：{}", emp);
        empService.updateByIdAndExpr(emp);
        return Result.success();
    }

    @Operation(summary = "查询全部员工", description = "获取所有员工列表")
    @GetMapping("/list")
    public Result list() {
        log.info("查询全部员工");
        return Result.success(empService.list());
    }
}
